
package com.mephi.gui;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JPanel;
import org.apache.log4j.Logger;


/**
 * 
 * It paints field of the chessboard
 * @author mephisto
 *
 */
public class Field extends JPanel 
{	
	/**
	 * Paints field with default white color, and size of field equal height of screen/12 
	 */
	public Field()
    {
		this(DEFAULT_FIELD_SIZE, DEFAULT_COLOR, null); 	
    }
	
	public Field(int fieldSize)
	{
		this(fieldSize, DEFAULT_COLOR, null); 	
	}
    
	//fake
    public Field(int fieldSize, Color fieldColor, Piece piece)
	{
    	super(new BorderLayout());
    	logger.debug("Constructing Field instance");
		setFieldSize(fieldSize);
		setSize(fieldSize, fieldSize);
		setFieldColor(fieldColor);
		setPiece(piece);
		setField(field);
		/*if (piece != null)
			this.add(piece, BorderLayout.CENTER);*/
		logger.debug("fieldColor: " + fieldColor);
		/*logger.debug("fieldSize: " + getWidth());
		
		logger.debug("piece: " + piece);*/
	}
    
    public Field(int fieldSize, Color fieldColor, Piece piece, String field)
	{
    	super(new BorderLayout());
    	logger.debug("Constructing Field instance");
		setFieldSize(fieldSize);
		setSize(fieldSize, fieldSize);
		setFieldColor(fieldColor);
		setPiece(piece);
		/*if (piece != null)
			this.add(piece, BorderLayout.CENTER);*/
		logger.debug("fieldColor: " + fieldColor);
		/*logger.debug("fieldSize: " + getWidth());
		
		logger.debug("piece: " + piece);*/
	}

    @Override
	protected void paintComponent(Graphics g) 
    {
    	setSize(fieldSize, fieldSize);
		//g.clearRect(0, 0, getHeight(), getWidth());
		g.setColor(getFieldColor());
		g.fillRect(0, 0, getWidth(), getHeight());
		
		int fontSize = 20;
	    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
	    g.setColor(Color.red);
		g.drawString(field, 600, 600);
		
		/*logger.debug("getWidth(): " + getWidth());
		logger.debug("getHeight(): " + getHeight());*/
    }
    
	/**
	 * @return the fieldSize
	 */
    public int getFieldSize()
	{
		return fieldSize;
	}

    /**
     * @param fieldSize the fieldSize to set
     * @throws IllegalArgumentException if the value of the fieldSize is not positive
     */
	public void setFieldSize(int fieldSize)
	{
		if (fieldSize <=0)
			throw new IllegalArgumentException("The value of the fieldSize must be positive!");
		this.fieldSize = fieldSize;
	}

	/**
	 * @return the fieldColor
	 */
	public Color getFieldColor()
	{
		return fieldColor;
	}

	 /**
     * @param fieldColor the fieldSize to set
     */
	public void setFieldColor(Color fieldColor)
	{
		this.fieldColor = fieldColor;
		repaint();
	}

	/**
	 * @return the piece or null if image of piece is not set
	 */
	public Piece getPiece()
	{
		return piece;
	}

	/**
     * @param piece the piece to set
     */
	public void setPiece(Piece piece)
	{
		if (piece != null)
		{
			this.piece = piece;
			this.add(piece, BorderLayout.CENTER);
			logger.debug("pieceSort: " + piece.getPieceSort());
			logger.debug("pieceColor: " + piece.getPieceColor());
			this.repaint();
		}
	}

	/**
	 * @return the field
	 */
	public String getField()
	{
		return field;
	}

	/**
	 * @param field the field to set
	 */
	public void setField(String field)
	{
		this.field = field;
	}


	private static Logger logger = Logger.getLogger("LOG");
	private static final long serialVersionUID = 8084839622581579473L;
    private static final int DEFAULT_FIELD_SIZE = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/12;
    private static Color DEFAULT_COLOR = Color.WHITE;
    private int fieldSize;
    private Color fieldColor;
    private Piece piece;
    private String field = "";
}
